
var express = require("express");
var Router = express.Router();  //创建一个路由

var { query } = require("../tool/conn.js");
// Router.get(routerPath,callback)   以get方式对路由进行拦截
// Router.post(routerPath,callback)  以post方式对路由进行拦截
// Router.all(routerPath,callback)   以所有方式对路由进行拦截

// routerPath 路由地址  可以是一级路由,也可以是二级路由
// callback   路由被访问时执行的回调函数

// /goods  => 查询所有数据 => 商品列表展示
Router.get("/", async function (req, res) {
    // res.send("/goods");
    var sql = `select id,name,img,price from goodslist`;
    var list = await query(sql);
    // console.log(list);
    if (list.length > 0) {
        res.json({
            status: true,
            message: "OK",
            list: list,
        })
    } else {
        res.json({
            status: true,
            message: "暂无数据",
            list: [],
        })
    }
})

// /goods/search    => 根据关键词搜索 排序  分页
Router.get("/search", function (req, res) {
    var data = req.query;
    var { wd, col = "id", type = "asc", page = 1, size = 10 } = data;
    if (wd != undefined && col && type && page && size) {

        // 查询 => 没有对页码大小进行限制
        // 页码最小值: 1
        // 页码最大值: 查询的数据 / size => 向上取值

        // 先查询满足条件的数据有多少条  => 有数据在排序和分页
        var search = `select id,name,img,price from goodslist where name like '%${wd}%'`;

        query(search).then(list => { // [{},{}]
            if (list.length > 0) {
                // 有数据 => 排序和分页
                var maxPage = Math.ceil(list.length / size);
                if (page > maxPage) page = maxPage;
                if (page < 1) page = 1;

                var sql = `select id,name,img,price from goodslist where name like '%${wd}%' order by ${col} ${type} limit ${(page - 1) * size},${size}`;

                query(sql).then(searchList => {
                    res.json({
                        status: true,
                        message: "OK",
                        total: list.length,
                        maxPage: maxPage,
                        current: page,
                        list: searchList,
                    })
                }).catch(err => {
                    res.json({
                        status: false,
                        message: err.message,
                    })
                })






            } else {
                res.json({
                    status: true,
                    message: "暂无数据",
                    list: [],
                })
            }
        }).catch(err => {
            res.json({
                status: false,
                message: err.message,
            })
        })



    } else {
        res.json({
            status: false,
            message: "请传入完整参数",
        })
    }

    // res.send("欢迎访问:/hello 路由");
})


// /goods/detail  => 根据传入的id显示商品详情信息
Router.get("/detail", async function (req, res) {
    var data = req.query;
    var { id: id } = data;
    var sql = `select id,name,img,price,smallPicList,bigPicList from goodslist where id = ${id}`;
    var list = await query(sql);
    // console.log(list);
    if (list.length > 0) {
        var info = list[0];
        res.json({
            status: true,
            message: "OK",
            data: info,
        })
    } else {
        res.json({
            status: true,
            message: "商品不存在",
            data: null
        })
    }
})

module.exports = Router;  //暴露创建的路由配置 (还需要配置到创建的服务中)

